home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mint110s / memprot.c < prev    next >
C/C++ Source or Header  |  1993-09-14  |  33KB  |  1,189 lines

  1. /*
  2.  * Copyright 1991,1992,1993 Atari Corporation.
  3.  * All rights reserved.
  4.  */
  5.  
  6. /*
  7.  * page-table data structures
  8.  *
  9.  *
  10.  * The root pointer points to a list of pointers to top-level pointer tables.
  11.  * 
  12.  * Each entry in a pointer table points to another pointer table or to
  13.  * a page table, or is a page descriptor.
  14.  * 
  15.  * Since, initially, the logical address space is the physical address space,
  16.  * we only need to worry about 26MB plus 32K for I/O space.
  17.  * 
  18.  * Since we want some pages to be supervisor-accessible, but we don't want
  19.  * a whole separate table for that, we use long-format descriptors.
  20.  * 
  21.  * Initial memory map:
  22.  * 
  23.  * 0     - membot: S (supervisor only)
  24.  * membot     - memtop: P (protected TPA)
  25.  * memtop     - phystop: G (screen)
  26.  * phystop     - $00E00000: bus error
  27.  * $00E00000- $00E3FFFF: G (ROM)
  28.  * $00E40000- $00FF7FFF: bus error
  29.  * $00FF8000- $00FFFFFF: G (mostly S: I/O space, but that's done in hardware)
  30.  * $01000000- ramtop: P
  31.  * ramtop     - $7FFFFFFF: G (A32/D32 VME, cacheable)
  32.  * $80000000- $FEFFFFFF: G (A32/D32 VME, non cacheable)
  33.  * $FFxxxxxx          just like $00xxxxxx.
  34.  * 
  35.  * Here's a final choice of layouts: IS=0, PS=13 (8K), TIA=4, TIB=4, TIC=4,
  36.  * TID=7.  This lets us map out entire unused megabytes at level C, and gives
  37.  * us an 8K page size, which is the largest the '040 can deal with.
  38.  * 
  39.  * This code implements 4+4+4+7, as follows:
  40.  * 
  41.  * tbl_a
  42.  *     0 -> tbl_b0
  43.  *     1-7 -> Cacheable direct (VME) page descriptors
  44.  *     8-E -> Non-cacheable direct (VME) page descriptors
  45.  *     F -> tbl_bf
  46.  * 
  47.  * tbl_b0 table: 16 entries (assumes only 16MB of TT RAM)
  48.  *     0 -> tbl_c00 (16MB of ST RAM address space)
  49.  *     1 -> tbl_c01 (16MB of TT RAM address space)
  50.  *     2-F -> cacheable direct (VME) page descriptors
  51.  * 
  52.  * tbl_bF table: 16 entries (deals with $FF mapping to $00)
  53.  *     0-E -> Non-cacheable direct (VME) page descriptors
  54.  *     F -> tbl_c00 (16MB of ST RAM address space, repeated here as $FF)
  55.  * 
  56.  * tbl_c00 table: ST RAM address space (example assuming 4MB ST RAM)
  57.  *     0-3 -> RAM page tables
  58.  *     4-D -> invalid
  59.  *     E -> direct map, cache enable (ROM)
  60.  *     F -> direct map, cache inhibit (I/O)
  61.  * 
  62.  * For each 16MB containing any TT RAM, there's a tbl_c.  Within those,
  63.  * for each MB that actually has TT RAM, there's another table, containing
  64.  * 128 RAM page tables.  Where there isn't RAM, there are "global"
  65.  * pages, to let the hardware bus error or not as it sees fit.
  66.  * 
  67.  * One RAM page table is allocated per megabyte of real RAM; each table has
  68.  * 128 entries, which is 8K per page.  For a TT with 4MB ST RAM and 4MB TT RAM
  69.  * that's 8K in page tables.  You can cut this down by not allocating page
  70.  * tables for which the entire megabyte is not accessible (i.e. it's all
  71.  * private memory and it's not YOUR private memory).
  72.  * 
  73.  * You have one of these per process.  When somebody loads into G or S memory
  74.  * or leaves it, you have to go through the page tables of every process
  75.  * updating S bits (for S) and DT (for G) bits.
  76.  * 
  77.  * The top levels are small & easy so replicating them once per process
  78.  * doesn't really hurt us.
  79.  * 
  80.  */
  81.  
  82. #include "mint.h"
  83.  
  84. #if 0
  85. #define MP_DEBUG(x) DEBUG(x)
  86. #else
  87. #define MP_DEBUG(x)
  88. #endif
  89.  
  90. void *memset P_((void *s, int ucharfill, unsigned long size));
  91. static void _dump_tree P_((long_desc tbl, int level));
  92.  
  93. extern int debug_level;        /* see debug.c */
  94. extern long mcpu;        /* in main.c */
  95.  
  96. /*
  97.  * You can turn this whole module off, and the stuff in context.s,
  98.  * by setting no_mem_prot to 1.
  99.  */
  100.  
  101. int no_mem_prot;
  102. long page_table_size;
  103.  
  104. /*
  105.  * PMMU stuff
  106.  */
  107.  
  108. /*
  109.  * This is one global TC register that is copied into every process'
  110.  * context, even though it never changes.  It's also used by the
  111.  * functions that dump page tables.
  112.  */
  113.  
  114. tc_reg tc;
  115.  
  116. /* mint_top_* get used in mem.c also */
  117. ulong mint_top_tt;
  118. ulong mint_top_st;
  119.  
  120. int tt_mbytes;        /* number of megabytds of TT RAM */
  121.  
  122. /*
  123.  * global_mode_table: one byte per page in the system.  Initially all pages
  124.  * are set to "global" but then the TPA pages are set to "invalid" in
  125.  * init_mem.  This has to be allocated and initialized in init_tables,
  126.  * when you know how much memory there is.  You need a byte per page,
  127.  * from zero to the end of TT RAM, including the space between STRAM
  128.  * and TTRAM.  That is, you need 16MB/pagesize plus (tt_mbytes/pagesize)
  129.  * bytes here.
  130.  */
  131.  
  132. unsigned char *global_mode_table;
  133.  
  134. /*
  135.  * prototype descriptors; field u1 must be all ones, other u? are zero.
  136.  * This is just the first long of a full descriptor; the ".page_type" part
  137.  * of the union.  These are initialized by init_tables.
  138.  *
  139.  * The proto_page_type table yields the value to stuff into the page_type
  140.  * field of a new process' page table.  It is the "non-owner" mode for
  141.  * a page with the corresponding value in global_mode_table.
  142.  */
  143.  
  144. page_type g_page;
  145. page_type g_ci_page;
  146. page_type s_page;
  147. page_type readable_page;
  148. page_type invalid_page;
  149. page_type page_ptr;
  150.  
  151. page_type *const proto_page_type[] =
  152.     { &invalid_page, &g_page, &s_page, &readable_page, &invalid_page };
  153. /*    private         global    super    private/read    invalid */
  154.  
  155. /*
  156.  * Init_tables: called sometime in initialization.  We set up some
  157.  * constants here, but that's all.  The first new_proc call will set up the
  158.  * page table for the root process and switch it in; from then on, we're
  159.  * always under some process' control.
  160.  * 
  161.  * The master page-mode table is initialized here, and some constants like
  162.  * the size needed for future page tables.
  163.  *
  164.  * One important constant initialized here is page_table_size, which is
  165.  * the amount of memory required per page table.  new_proc allocates
  166.  * this much memory for each process' page table.  This number will be
  167.  * 1K/megabyte plus page table overhead.  There are TBL_PAGES_OFFS
  168.  * tables at TBL_SIZE_BYTES each before the main tables begin; then
  169.  * there is 1024 bytes per megabyte of memory being mapped.
  170.  */
  171.  
  172. void
  173. init_tables()
  174. {
  175.     int n_megabytes;
  176.     long global_mode_table_size;
  177.  
  178.     if (no_mem_prot) return;
  179.  
  180.     TRACE(("init_tables"));
  181.  
  182. #define phys_top_tt (*(ulong *)0x5a4L)
  183.     if (phys_top_tt == 0x01000000L) mint_top_tt = 0;
  184.     else mint_top_tt = phys_top_tt;
  185.  
  186. #define phys_top_st (*(ulong *)0x42eL)
  187.     mint_top_st = phys_top_st;
  188.  
  189.     if (mint_top_tt)
  190.         tt_mbytes = (int) ((mint_top_tt - 0x01000000L) / ONE_MEG);
  191.     else
  192.         tt_mbytes = 0;
  193.  
  194.     n_megabytes = (int) ((mint_top_st / ONE_MEG) + tt_mbytes);
  195.  
  196.     /*
  197.      * page table size: room for A table, B0 table, BF table, STRAM C
  198.      * table, one TTRAM C table per 16MB (or fraction) of TTRAM, and 1024
  199.      * bytes per megabyte.
  200.      */
  201.  
  202.     page_table_size = (4L * TBL_SIZE_BYTES) +
  203.               (((tt_mbytes+15L)/16L) * TBL_SIZE_BYTES) +
  204.               (n_megabytes*1024L);
  205.  
  206.     global_mode_table_size = ((SIXTEEN_MEG / QUANTUM) +
  207.                 (((ulong)tt_mbytes * ONE_MEG) / QUANTUM));
  208.  
  209.     global_mode_table = kmalloc(global_mode_table_size);
  210.  
  211.     assert(global_mode_table);
  212.  
  213.     TRACELOW(("mint_top_st is $%lx; mint_top_tt is $%lx, n_megabytes is %d",
  214.     mint_top_st, mint_top_tt, n_megabytes));
  215.     TRACELOW(("page_table_size is %ld, global_mode_table_size %ld",
  216.         page_table_size,
  217.         global_mode_table_size));
  218.  
  219.     g_page.limit = 0x7fff;    /* set nonzero fields: disabled limit */
  220.     g_page.unused1 = 0x3f;    /* ones in this reserved field */
  221.     g_page.unused2 = 0;
  222.     g_page.s = 0;
  223.     g_page.unused3 = 0;
  224.     g_page.ci = 0;
  225.     g_page.unused4 = 0;
  226.     g_page.m = 1;        /* set m and u to 1 so CPU won't do writes */
  227.     g_page.u = 1;
  228.     g_page.wp = 0;        /* not write-protected */
  229.     g_page.dt = 1;        /* descriptor type 1: page descriptor */
  230.  
  231.     g_ci_page = g_page;
  232.     g_ci_page.ci = 1;
  233.  
  234.     readable_page = g_page;    /* a page which is globally readable */
  235.     readable_page.wp = 1;    /* but write protected */
  236.  
  237.     s_page = g_page;        /* a page which is globally accessible */
  238.     s_page.s = 1;        /* if you're supervisor */
  239.  
  240.     invalid_page = g_page;
  241.     invalid_page.dt = 0;
  242.  
  243.     page_ptr = g_page;
  244.     page_ptr.m = 0;        /* this must be zero in page pointers */
  245.     page_ptr.dt = 3;
  246.  
  247.     tc.enable = 1;
  248.     tc.zeros = 0;
  249.     tc.sre = 0;
  250.     tc.fcl = 0;
  251.     tc.is = 0;
  252.     tc.tia = 4;
  253.     tc.tib = 4;
  254.     tc.tic = 4;
  255.     tc.tid = 7;            /* 0+4+4+4+7+13 == 32 */
  256.     tc.ps = 13;            /* 8K page size */
  257.  
  258.     /* set the whole global_mode_table to "global" */
  259.     memset(global_mode_table,PROT_G,global_mode_table_size);
  260. }
  261.  
  262. /*
  263.  * mark_region: mark a region of memory as having a particular type.
  264.  * The arguments are the memory region in question and the new type.
  265.  * If the new type is zero then the old type is preserved.  The
  266.  * type of each page is kept in a global place for this purpose,
  267.  * among others.
  268.  *
  269.  * The types are:
  270.  *  0    private
  271.  *  1    global
  272.  *  2    private, but super-accessible
  273.  *  3    private, but world readable
  274.  *  4   invalid
  275.  *
  276.  
  277. The idea is this:
  278.  
  279.     for (each process) {
  280.     if (you're an owner or you're special) {
  281.         set up owner modes
  282.     }
  283.     else {
  284.         set up non-owner modes
  285.     }
  286.  
  287.     mark_pages(pagetbl,start,len,modes);
  288.     }
  289.  
  290.  */
  291.  
  292. /*
  293.                 invalid---v
  294.               private/gr---v  |
  295.                 super-------v  |  |
  296.             global-------v  |  |  |
  297.         private-------v     |  |  |  |
  298.                   |     |  |  |  |
  299. */
  300. const ushort other_dt[]    =   { 0, 1, 1, 1, 0 };
  301. const ushort other_s[] =    { 0, 0, 1, 0, 0 };
  302. const ushort other_wp[] =   { 0, 0, 0, 1, 0 };
  303.  
  304.  
  305. /*
  306.  * get_page_cookie: return a cookie representing the protection status
  307.  * of some memory.
  308.  *
  309.  * Returns ((wp << 3) | (s << 2) | (dt) | 0x8000) when it wins.
  310.  * Returns 1 if the pages are not all controlled, 0 if they're not all the same.
  311.  */
  312.  
  313. static short
  314. get_page_cookie(long_desc *base_tbl,ulong start,ulong len)
  315. {
  316.     int b_index, c_index, d_index;
  317.     long_desc *tbl, *tbl_b, *tbl_c;
  318.     int dt, s, wp;
  319.  
  320.     if (start < mint_top_st) {
  321.     /* start is in ST RAM; fail if not entirely in ST RAM */
  322.     if (start+len > mint_top_st) {
  323.         return 1;
  324.     }
  325.     }
  326.     else if (start >= 0x01000000L && start < mint_top_tt) {
  327.     /* start is in TT RAM; fail if not entirely in TT RAM */
  328.     if (start+len > mint_top_tt) {
  329.         return 1;
  330.     }
  331.     }
  332.  
  333.     /*
  334.      * a_index is always zero.  Only the first 256MB is mapped.
  335.      * b_index is the 16MB number of the page.
  336.      * c_index is the 1MB number of that page within the 16MB (0-15)
  337.      * d_index is the 8K number within that 1MB (0-127).
  338.      */
  339.  
  340.     b_index = (int)(start >> LOG2_16_MEG);
  341.     c_index = (int)(start >> LOG2_ONE_MEG) & 0xf;
  342.     d_index = (int)(start >> LOG2_EIGHT_K) & 0x7f;
  343.  
  344.     /* precompute the table addresses */
  345.     tbl_b = &base_tbl[0].tbl_address[b_index];
  346.     tbl_c = &tbl_b->tbl_address[c_index];
  347.     tbl = &tbl_c->tbl_address[d_index];
  348.  
  349.     dt = tbl->page_type.dt;
  350.     wp = tbl->page_type.wp;
  351.     s = tbl->page_type.s;
  352.  
  353.     for (;;) {
  354.     /* quickly loop through the 1MB-block */
  355.     for (; len && tbl < &tbl_c->tbl_address[0x80]; tbl++)
  356.       {
  357.         if ((tbl->page_type.dt != dt) ||
  358.         (tbl->page_type.s != s) ||
  359.         (tbl->page_type.wp != wp)) {
  360.             /* fail because it's not all the same protection */
  361.             return 0;
  362.         }
  363.         len -= EIGHT_K;
  364.       }
  365.  
  366.     if (len == 0L)
  367.       break;
  368.  
  369.     /* step to the next d-table */
  370.     tbl_c++;
  371.     /* if crossing a 16MB boundary, get the next c-table */
  372.     if (tbl_c == &tbl_b->tbl_address[0x10])
  373.       {
  374.         tbl_b++;
  375.         tbl_c = tbl_b->tbl_address;
  376.       }
  377.     tbl = tbl_c->tbl_address;
  378.     }
  379.     /* we passed -- all the pages in question have the same prot. status */
  380.     return (wp << 3) | (s << 2) | dt | 0x8000;
  381. }
  382.  
  383. static void
  384. mark_pages(long_desc *base_tbl,ulong start,ulong len,
  385.         ushort dt_val, ushort s_val, ushort wp_val, PROC *proc)
  386. {
  387.     int b_index, c_index, d_index;
  388.     long_desc *tbl, *tbl_b, *tbl_c;
  389.     ulong oldlen;
  390.  
  391.     if (no_mem_prot) return;
  392.  
  393.     oldlen = len;
  394.  
  395.     /*
  396.      * a_index is always zero.  Only the first 256MB is mapped.
  397.      * b_index is the 16MB number of the page.
  398.      * c_index is the 1MB number of that page within the 16MB (0-15)
  399.      * d_index is the 8K number within that 1MB (0-127).
  400.      */
  401.  
  402.     b_index = (start >> LOG2_16_MEG);
  403.     c_index = (start >> LOG2_ONE_MEG) & 0xf;
  404.     d_index = (start >> LOG2_EIGHT_K) & 0x7f;
  405.  
  406.     /* precompute the table addresses */
  407.     tbl_b = &base_tbl[0].tbl_address[b_index];
  408.     tbl_c = &tbl_b->tbl_address[c_index];
  409.     tbl = &tbl_c->tbl_address[d_index];
  410.  
  411. #ifdef MEMPROT_SHORTCUT
  412.     /*
  413.      * Take a shortcut here: we're done if first page of the region is
  414.      * already right.
  415.      */
  416.     /* I don't think this shortcut is a good idea, since while we
  417.      * are doing Mshrink or Srealloc we may very well have a region
  418.      * with mixed page types -- ERS
  419.      */
  420.  
  421.     if (tbl->page_type.dt == dt_val &&
  422.     tbl->page_type.s == s_val &&
  423.     tbl->page_type.wp == wp_val) {
  424. /*
  425.         TRACE(("mark_pages a:0 b:%d c:%d d:%d (same)",
  426.             b_index,c_index,d_index));
  427. */
  428.         return;
  429.     }
  430.  
  431. #endif /* MEMPROT_SHORTCUT */
  432. /*
  433.     MP_DEBUG(("mark_pages a:0 b:%d c:%d d:%d (diff)",b_index,c_index,d_index));
  434. */
  435.  
  436.     for (;;)
  437.       {
  438.     /* quickly loop through the 1MB-block */
  439.     for (; len && tbl < &tbl_c->tbl_address[0x80]; tbl++)
  440.       {
  441.         tbl->page_type.dt = dt_val;
  442.         tbl->page_type.s = s_val;
  443.         tbl->page_type.wp = wp_val;
  444.         len -= EIGHT_K;
  445.       }
  446.  
  447.     if (len == 0L)
  448.       break;
  449.  
  450.     /* get the next d-table */
  451.     tbl_c++;
  452.     /* if crossing a 16MB boundary, get the next c-table */
  453.     if (tbl_c == &tbl_b->tbl_address[0x10])
  454.       {
  455.         tbl_b++;
  456.         tbl_c = tbl_b->tbl_address;
  457.       }
  458.     tbl = tbl_c->tbl_address;
  459.     }
  460.  
  461.     flush_pmmu();
  462.     if (mcpu <= 30) {
  463.     /* On the '020 & '030 we have a logical cache, i.e. the DC & IC are on
  464.      * the CPU side of the MMU, hence on an MMU context switch we must flush
  465.      * them too. On the '040, by comparison, we have a physical cache, i.e.
  466.      * the DC & IC are on the memory side of the MMU, so no DC/IC cache flush
  467.      * is needed.
  468.      */
  469.     cpush((void *)start, oldlen);
  470.     }
  471. }
  472.  
  473. /* get_prot_mode(r): returns the type of protection region r
  474.  * has
  475.  */
  476.  
  477. int
  478. get_prot_mode(r)
  479.     MEMREGION *r;
  480. {
  481.     ulong start = r->loc;
  482.  
  483.     if (no_mem_prot)
  484.         return PROT_G;
  485.     return global_mode_table[(start >> 13)];
  486. }
  487.  
  488. void
  489. mark_region(region,mode)
  490. MEMREGION *region;
  491. short mode;
  492. {
  493.     ulong start = region->loc;
  494.     ulong len = region->len;
  495.     ulong i;
  496.     ushort dt_val, s_val, wp_val;
  497.     PROC *proc;
  498.     MEMREGION **mr;
  499.  
  500.     if (no_mem_prot) return;
  501.  
  502.     MP_DEBUG(("mark_region %lx len %lx mode %d",start,len,mode));
  503.     
  504. #if 0 /* this should not occur any more */
  505.     if (mode == PROT_NOCHANGE) {
  506.     mode = global_mode_table[(start >> 13)];
  507.     }
  508. #else
  509.     assert(mode != PROT_NOCHANGE);
  510. #endif
  511.  
  512.     /* mark the global page table */
  513.  
  514.     memset(&global_mode_table[start >> 13],mode,(len >> 13));
  515.  
  516.     for (proc = proclist; proc; proc = proc->gl_next) {
  517.     assert(proc->page_table);
  518.     if (mode == PROT_I || mode == PROT_G) {
  519.         /* everybody gets the same flags */
  520.         goto notowner;
  521.     }
  522.     if (proc->memflags & F_OS_SPECIAL) {
  523.         /* you're special; you get owner flags */
  524.         MP_DEBUG(("mark_region: pid %d is an OS special!",proc->pid));
  525.         goto owner;
  526.     }
  527.     if ((mr = proc->mem) != 0) {
  528.         for (i = 0; i < proc->num_reg; i++, mr++) {
  529.         if (*mr == region) {
  530.             MP_DEBUG(("mark_region: pid %d is an owner",proc->pid));
  531. owner:
  532.             dt_val = 1;
  533.             s_val = 0;
  534.             wp_val = 0;
  535.             goto gotvals;
  536.         }
  537.         }
  538.     }
  539.  
  540. notowner:
  541.  
  542. /* if you get here you're not an owner, or mode is G or I */
  543.     MP_DEBUG(("mark_region: pid %d gets non-owner modes",proc->pid));
  544.  
  545.     dt_val = other_dt[mode];
  546.     s_val = other_s[mode];
  547.     wp_val = other_wp[mode];
  548.  
  549. gotvals:
  550.     mark_pages(proc->page_table,start,len,dt_val,s_val,wp_val,proc);
  551.     }
  552. }
  553.  
  554. /* special version of mark_region, used for attaching (mode == PROT_P)
  555.    and detaching (mode == PROT_I) a memory region to/from a process. */
  556. void
  557. mark_proc_region(proc,region,mode)
  558. PROC *proc;
  559. MEMREGION *region;
  560. short mode;
  561. {
  562.     ulong start = region->loc;
  563.     ulong len = region->len;
  564.     ushort dt_val, s_val, wp_val;
  565.     short global_mode;
  566.  
  567.     if (no_mem_prot) return;
  568.  
  569.     MP_DEBUG(("mark_region %lx len %lx mode %d for pid %d",
  570.           start, len, mode, proc->pid));
  571.  
  572.     global_mode = global_mode_table[(start >> 13)];
  573.  
  574.     assert(proc->page_table);
  575.     if (global_mode == PROT_I || global_mode == PROT_G)
  576.       mode = global_mode;
  577.     else {
  578.     if (proc->memflags & F_OS_SPECIAL) {
  579.         /* you're special; you get owner flags */
  580.         MP_DEBUG(("mark_region: pid %d is an OS special!",proc->pid));
  581.         goto owner;
  582.     }
  583.     if (mode == PROT_P) {
  584.         MP_DEBUG(("mark_region: pid %d is an owner",proc->pid));
  585. owner:
  586.         dt_val = 1;
  587.         s_val = 0;
  588.         wp_val = 0;
  589.         goto gotvals;
  590.     }
  591.     }
  592.  
  593. /* if you get here you're not an owner, or mode is G or I */
  594.     MP_DEBUG(("mark_region: pid %d gets non-owner modes",proc->pid));
  595.  
  596.     dt_val = other_dt[mode];
  597.     s_val = other_s[mode];
  598.     wp_val = other_wp[mode];
  599.  
  600. gotvals:
  601.     mark_pages(proc->page_table,start,len,dt_val,s_val,wp_val,proc);
  602. }
  603.  
  604. /*
  605.  * prot_temp: temporarily alter curproc's access to memory.
  606.  * Pass in a -1 to give curproc global access; returns a cookie.  Call
  607.  * again with that cookie to return the memory to the old mode.
  608.  * There should be no context switches or memory protection changes
  609.  * in the meantime.
  610.  *
  611.  * If called with mode == -1, returns...
  612.  *    -1 if mem prot is off -- no error, no action.
  613.  *     0 if the pages are not all the same.
  614.  *     1 if the pages are not all controlled by the page tables.
  615.  *
  616.  * When mode != -1, returns...
  617.  *    0 for success (should never fail).  There is little checking.
  618.  * Calling with mode == 0 or 1 results in zero to spoof success, but in fact
  619.  * this is an error.  Mode is only really valid if (mode & 0x8000).
  620.  */
  621.  
  622. int
  623. prot_temp(loc,len,mode)
  624. ulong loc;
  625. ulong len;
  626. int mode;
  627. {
  628.     int cookie;
  629.  
  630.     if (no_mem_prot) return -1;
  631.  
  632.     /* round start down to the previous page and len up to the next one. */
  633.     len += loc & MASKBITS;
  634.     loc &= ~MASKBITS;
  635.     len = ROUND(len);
  636.  
  637.     if (mode == 0 || mode == 1) return 0;    /* do nothing */
  638.     if (mode == -1) {
  639.     cookie = get_page_cookie(curproc->page_table,loc,len);
  640.  
  641.     /* if not all controlled, return status */
  642.     if (cookie == 0 || cookie == 1) return cookie;
  643.  
  644.     mark_pages(curproc->page_table,loc,len,1,0,0,curproc);
  645.  
  646.     return cookie;
  647.     }
  648.     else {
  649.     mark_pages(curproc->page_table,loc,len,
  650.             mode&3,(mode&4)>>2,(mode&8)>>3,curproc);
  651.     return 0;
  652.     }
  653. }
  654.  
  655. /*
  656.  * init_page_table: fill in the page table for the indicated process. The
  657.  * master page map is consulted for the modes of all pages, and the memory
  658.  * region data structures are consulted to see if this process is the owner
  659.  * of any of those tables.
  660.  *
  661.  * This also sets crp and tc in both ctxts of the process.  If this is the
  662.  * first call, then the CPU tc is cleared, the TT0 and TT1 regs are zapped,
  663.  * and then this proc's crp and tc are loaded into it.
  664.  */
  665.  
  666. static short mmu_is_set_up = 0;
  667.  
  668. void
  669. init_page_table(proc)
  670. PROC *proc;
  671. {
  672.     long_desc *tptr;
  673.     long_desc *tbl_a;        /* top-level table */
  674.     long_desc *tbl_b0;        /* second level, handles $0 nybble */
  675.     long_desc *tbl_bf;        /* handles $F nybble */
  676.     long_desc *tbl_c;        /* temp pointer to start of 16MB */
  677.     ulong p, q, r;
  678.     ulong i, j, k;
  679.     int g;
  680.     MEMREGION **mr;
  681.  
  682.     if (no_mem_prot) return;
  683.  
  684.     assert(proc && proc->page_table);
  685.  
  686.     if (proc->pid)
  687.         TRACELOW(("init_page_table(proc=%lx, pid %d)",proc,proc->pid));
  688.  
  689.     tptr = proc->page_table;
  690.     tbl_a = tptr;
  691.     tptr += TBL_SIZE;
  692.     tbl_b0 = tptr;
  693.     tptr += TBL_SIZE;
  694.     tbl_bf = tptr;
  695.     tptr += TBL_SIZE;
  696.  
  697.     /*
  698.      * table A indexes by the first nybble: $0 and $F refer to their tables,
  699.      * $1-$7 are uncontrolled, cacheable; $8-$E are uncontrolled, ci.
  700.      */
  701.  
  702.     tbl_a[0].page_type = page_ptr;
  703.     tbl_a[0].tbl_address = tbl_b0;
  704.  
  705.     for (i=1; i<0xf; i++) {
  706.     if (i < 8)  tbl_a[i].page_type = g_page;
  707.     else        tbl_a[i].page_type = g_ci_page;
  708.     tbl_a[i].tbl_address = (long_desc *)(i << 28);
  709.     }
  710.  
  711.     /* $F entry of table A refers to table BF */
  712.     tbl_a[0xf].page_type = page_ptr;
  713.     tbl_a[0xf].tbl_address = tbl_bf;
  714.  
  715.     /*
  716.      * table B0: entry 0 is $00, the 16MB of ST address space.
  717.      */
  718.  
  719.     tbl_b0[0].page_type = page_ptr;
  720.     tbl_b0[0].tbl_address = tptr;
  721.     tbl_c = tptr;
  722.     tptr += TBL_SIZE;
  723.  
  724.     /* for each megabyte that is RAM, allocate a table */
  725.     for (i = 0, k = 0, p = 0; p < mint_top_st; i++, p += 0x00100000L) {
  726.     tbl_c[i].page_type = page_ptr;
  727.     tbl_c[i].tbl_address = tptr;
  728.  
  729.     /* for each page in this megabyte, write a page entry */
  730.     for (q = p, j = 0; j < 128; j++, q += 0x2000, k++) {
  731.         tptr->page_type = *proto_page_type[global_mode_table[k]];
  732.         tptr->tbl_address = (long_desc *)q;
  733.         tptr++;
  734.     }
  735.     }
  736.  
  737.     /* now for each megabyte from mint_top_st to ROM, mark global */
  738.     for ( ; p < 0x00E00000L; i++, p += 0x00100000L) {
  739.     tbl_c[i].page_type = g_page;
  740.     tbl_c[i].tbl_address = (long_desc *)p;
  741.     }
  742.  
  743.     /* fill in the E and F tables: 00Ex is ROM, 00Fx is I/O  */
  744.     tbl_c[i].page_type = g_page;
  745.     tbl_c[i].tbl_address = (long_desc *)p;
  746.     i++, p += 0x00100000L;
  747.     tbl_c[i].page_type = g_ci_page;
  748.     tbl_c[i].tbl_address = (long_desc *)p;
  749.  
  750.         /* Done with tbl_c for 0th 16MB; go on to TT RAM */
  751.  
  752. /* 
  753.     structure:
  754.  
  755.     for (i = each 16MB that has any TT RAM in it)
  756.     allocate a table tbl_c, point tbl_b0[i] at it
  757.     for (j = each 1MB that is RAM)
  758.         allocate a table, point tbl_c[j] at it
  759.         for (k = each page in the megabyte)
  760.         fill in tbl_c[j][k] with page entry from global_mode_table
  761.     for (j = the rest of the 16MB)
  762.         set tbl_c[j] to "global, cacheable"
  763.  
  764.     for (i = the rest of the 16MBs from here to $7F)
  765.     set tbl_b0[i] to "global, cacheable"
  766.  
  767.     for (i = the rest of the 16MBs from $80 up to but not incl. $FF)
  768.     set tbl_b0[i] to "global, not cacheable"
  769. */
  770.  
  771.     /* i counts 16MBs */
  772.     for (i = 1, p = 0x01000000L, g = 2048;
  773.      p < mint_top_tt;
  774.      p += SIXTEEN_MEG, i++) {
  775.         tbl_b0[i].page_type = page_ptr;
  776.         tbl_b0[i].tbl_address = tptr;
  777.         tbl_c = tptr;
  778.         tptr += TBL_SIZE;
  779.  
  780.         /* j counts MBs */
  781.         for (j = 0, q = p; j < 16 && q < mint_top_tt; q += ONE_MEG, j++) {
  782.         tbl_c[j].page_type = page_ptr;
  783.         tbl_c[j].tbl_address = tptr;
  784.         /* k counts pages (8K) */
  785.         for (r = q, k = 0; k < 128; k++, r += 0x2000, g++) {
  786.             tptr->page_type = *proto_page_type[global_mode_table[g]];
  787.             tptr->tbl_address = (long_desc *)r;
  788.             tptr++;
  789.         }
  790.         }
  791.         for ( ; j < 16; j++, q += ONE_MEG) {
  792.         /* fill in the rest of this 16MB */
  793.         tbl_c[j].page_type = g_page;
  794.         tbl_c[j].tbl_address = (long_desc *)q;
  795.         }
  796.     }
  797.  
  798.     /* fill in the rest of $00-$0F as cacheable */
  799.     for ( ; i < 16; i++, p += SIXTEEN_MEG) {
  800.     tbl_b0[i].page_type = g_page;
  801.     tbl_b0[i].tbl_address = (long_desc *)p;
  802.     }
  803.  
  804.     /* done with TT RAM in table b0; do table bf */
  805.  
  806.     /*
  807.      * Table BF: translates addresses starting with $F.  First 15 are
  808.      * uncontrolled, cacheable; last one translates $FF, which
  809.      * which shadows $00 (the 16MB ST address space).  The rest
  810.      * are uncontrolled, not cacheable.
  811.      *
  812.      * The table address of the copy has a 1 in the low (unused) bit, which
  813.      * is a signal to the table dumper not to dump this, as it's a copy
  814.      * of tbl_b0[0].
  815.      */
  816.  
  817.     for (i=0; i<0xf; i++) {
  818.     tbl_bf[i].page_type = g_ci_page;
  819.     tbl_bf[i].tbl_address = (long_desc *)((i << 24) | 0xf0000000L);
  820.     }
  821.     tbl_bf[0xf] = tbl_b0[0];
  822.     *(ulong *)(&(tbl_bf[0xf].tbl_address)) |= 1;
  823.  
  824.     proc->ctxt[0].crp.limit = 0x7fff;    /* disable limit function */
  825.     proc->ctxt[0].crp.dt = 3;        /* points to valid 8-byte entries */
  826.     proc->ctxt[0].crp.tbl_address = tbl_a;
  827.     proc->ctxt[1].crp = proc->ctxt[0].crp;
  828.     proc->ctxt[0].tc = tc;
  829.     proc->ctxt[1].tc = tc;
  830.  
  831.     /*
  832.      * OK, memory tables are now there as if you're a non-owner of every
  833.      * page.  Now for each region you ARE an owner of, mark with owner
  834.      * modes.
  835.      */
  836.  
  837.     mr = proc->mem;
  838.     for (i=0; i < proc->num_reg; i++, mr++) {
  839.     if (*mr) {
  840.             mark_pages(proc->page_table,(*mr)->loc,(*mr)->len,1,0,0,proc);
  841.         }
  842.     }
  843.  
  844.     if (!mmu_is_set_up) {
  845.     set_mmu(proc->ctxt[0].crp,proc->ctxt[0].tc);
  846.     mmu_is_set_up = 1;
  847.     }
  848. }
  849.  
  850. /*
  851.  * This routine is called when procfs detects that a process wants to be an
  852.  * OS SPECIAL.  The AES, SCRENMGR, and DESKTOP do this, and so does FSMGDOS
  853.  * and possibly some other stuff. It has to re-mark every page in that
  854.  * process' page table based on its new special status. The "special
  855.  * status" is "you get global access to all of memory" and "everybody
  856.  * gets Super access to yours."  It is the caller's responsibility
  857.  * to set proc's memflags, usually to (F_OS_SPECIAL | F_PROT_S).
  858.  */
  859.  
  860. void
  861. mem_prot_special(proc)
  862. PROC *proc;
  863. {
  864.     MEMREGION **mr;
  865.     int i;
  866.  
  867.     if (no_mem_prot) return;
  868.  
  869.     TRACE(("mem_prot_special(pid %d)",proc->pid));
  870.  
  871.     /*
  872.      * This marks ALL memory, allocated or not, as accessible. When memory
  873.      * is freed even F_OS_SPECIAL processes lose access to it. So one or
  874.      * the other of these is a bug, depending on how you want it to work.
  875.      */
  876.     mark_pages(proc->page_table,0,mint_top_st,1,0,0,proc);
  877.     if (mint_top_tt) {
  878.     mark_pages(proc->page_table,
  879.             0x01000000L,
  880.             mint_top_tt - 0x01000000L,
  881.             1,0,0,
  882.             proc);
  883.     }
  884.  
  885.     /*
  886.      * In addition, mark all the pages the process already owns as "super"
  887.      * in all other processes.  Thus the "special" process can access all
  888.      * of memory, and any process can access the "special" process' memory
  889.      * when in super mode.
  890.      */
  891.  
  892.     mr = proc->mem;
  893.  
  894.     for (i=0; i < proc->num_reg; i++, mr++) {
  895.     if (*mr) {
  896.         mark_region(*mr,PROT_S);
  897.     }
  898.     }
  899. }
  900.     
  901. /*----------------------------------------------------------------------------
  902.  * DEBUGGING SECTION
  903.  *--------------------------------------------------------------------------*/
  904.  
  905. static void
  906. _dump_tree(tbl,level)
  907. long_desc tbl;
  908. int level;
  909. {
  910.     int i, j;
  911.     long_desc *p;
  912.     static const char spaces[9] = "        ";
  913.  
  914.     /* print the level and display the table descriptor */
  915.     FORCE("\r%s s:%x wp:%x dt:%x a:%08lx",
  916.     &spaces[8-(level*2)],
  917.     tbl.page_type.s,
  918.     tbl.page_type.wp,
  919.     tbl.page_type.dt,
  920.     tbl.tbl_address);
  921.  
  922.     if (tbl.page_type.dt == 3) {
  923.     if (level == 0) {
  924.         j = (1 << tc.tia);
  925.     }
  926.     else if (level == 1) {
  927.         j = (1 << tc.tib);
  928.     }
  929.     else if (level == 2) {
  930.         j = (1 << tc.tic);
  931.     }
  932.     else {
  933.         j = (1 << tc.tid);
  934.     }
  935.  
  936.     /* don't show table if it's the duplicate */
  937.     if ((ulong)tbl.tbl_address & 1) return;
  938.  
  939.     ++level;
  940.     p = tbl.tbl_address;
  941.     for (i=0; i<j; i++, p++) {
  942.         _dump_tree(*p,level);
  943.     }
  944.     }
  945. }
  946.  
  947. static const char modesym[] = { 'p', 'g', 's', 'r', 'i' };
  948.  
  949. void
  950. QUICKDUMP()
  951. {
  952.     char outstr[33];
  953.     ulong i, j, end;
  954.  
  955.     if (no_mem_prot) return;
  956.  
  957.     FORCE("STRAM global table:");
  958.     outstr[32] = '\0';
  959.     end = mint_top_st / QUANTUM;
  960.     for (i = 0; i < end; i += 32) {
  961.     for (j=0; j<32; j++) {
  962.         outstr[j] = modesym[global_mode_table[j+i]];
  963.     }
  964.     FORCE("%08lx: %s",i*8192L,outstr);
  965.     }
  966.  
  967.     if (mint_top_tt) {
  968.     FORCE("TTRAM global table:");
  969.     end = mint_top_tt / QUANTUM;
  970.     for (i = 2048; i < end; i += 32) {
  971.         for (j=0; j<32; j++) {
  972.         outstr[j] = modesym[global_mode_table[j+i]];
  973.         }
  974.         FORCE("%08lx: %s",i*8192L,outstr);
  975.     }
  976.     }
  977. }
  978.  
  979. const char *berr_msg[] = { 
  980. /*  "........." */
  981.     "private  ",
  982.     "global   ",    /* turned into "hardware" for violation reports */
  983.     "super    ",
  984.     "readable ",
  985.     "free     ",
  986.     "hardware "        /* used when the memory is not controlled by us */
  987. };
  988.  
  989. void
  990. report_buserr()
  991. {
  992.     const char *vmsg;
  993.     short mode;
  994.     ulong aa, pc;
  995.     char alertbuf[5*32+16];    /* enough for an alert */
  996.     char *aptr;
  997.  
  998.     if (no_mem_prot) return;
  999.  
  1000.     aa = curproc->exception_addr;
  1001.     pc = curproc->exception_pc;
  1002.     if ((mint_top_tt && aa < mint_top_tt) || (aa < mint_top_st)) {
  1003.     mode = global_mode_table[(curproc->exception_addr >> 13)];
  1004.     if (mode == PROT_G) {
  1005.         /* page is global: obviously a hardware bus error */
  1006.         mode = 5;
  1007.     }
  1008.     }
  1009.     else {
  1010.     /* (addr is > mint_top_tt) set mode = 5 so we don't look for owners */
  1011.         mode = 5;
  1012.     }
  1013.     vmsg = berr_msg[mode];
  1014.  
  1015.     /* construct an AES alert box for this error:
  1016.     | PROCESS  "buserrxx"  KILLED: |
  1017.     | MEMORY VIOLATION.  (PID 000) |
  1018.     |                              |
  1019.     | Type: ......... PC: pc...... |
  1020.     | Addr: ........  BP: ........ |
  1021.     */
  1022.  
  1023.     /* we play games to get around 128-char max for ksprintf */
  1024.     ksprintf(alertbuf,"[1][ PROCESS  \"%s\"  KILLED: |",curproc->name);
  1025.     aptr = alertbuf + strlen(alertbuf);
  1026.     ksprintf(aptr," MEMORY VIOLATION.  (PID %03d) | |",curproc->pid);
  1027.     aptr = alertbuf + strlen(alertbuf);
  1028.     ksprintf(aptr," Type: %s PC: %08lx |",vmsg,pc);
  1029.     aptr = alertbuf + strlen(alertbuf);
  1030.     ksprintf(aptr," Addr: %08lx  BP: %08lx ][ OK ]",aa,curproc->base);
  1031.     if (!_ALERT(alertbuf)) {
  1032.         /* this will call _alert again, but it will just fail again */
  1033.         ALERT("MEMORY VIOLATION: type=%s AA=%lx PC=%lx BP=%lx",
  1034.             vmsg,aa,pc,curproc->base);
  1035.     }
  1036.         
  1037.     if (curproc->pid == 0 || curproc->memflags & F_OS_SPECIAL) {
  1038.     /* the system is so thoroughly hosed that anything we try will
  1039.      * likely cause another bus error; so let's just hang up
  1040.      */
  1041.     FATAL("Operating system killed");
  1042.     }
  1043. }
  1044.  
  1045. /*
  1046.  * big_mem_dump is a biggie: for each page in the system, it
  1047.  * displays the PID of the (first) owner and the protection mode.
  1048.  * The output has three chars per page, and eight chars per line.
  1049.  * The first page of a region is marked with the mode, and the
  1050.  * rest with a space.
  1051.  *
  1052.  * Logic:
  1053.     for (mp = *core; mp; mp++) {
  1054.     for (each page of this region) {
  1055.         if (start of line) {
  1056.         output line starter;
  1057.         }
  1058.         if (start of region) {
  1059.         output mode of this page;
  1060.         determine owner;
  1061.         output owner;
  1062.         }
  1063.         else {
  1064.         output space;
  1065.         output owner;
  1066.         }
  1067.         }
  1068.     }
  1069.  */
  1070.  
  1071. void
  1072. BIG_MEM_DUMP(bigone,proc)
  1073. int bigone;
  1074. PROC *proc;
  1075. {
  1076. #ifdef DEBUG_INFO
  1077.     char linebuf[128];
  1078.     char *lp = linebuf;
  1079.     MEMREGION *mp, **mr, **map;
  1080.     PROC *p;
  1081.     ulong loc;
  1082.     short owner;
  1083.     short i;
  1084.     short first;
  1085.  
  1086.  
  1087.   if (no_mem_prot) return;
  1088.  
  1089.   for (map = core; map != 0; ((map == core) ? (map = alt) : (map = 0))) {
  1090.     FORCE("Annotated memory dump for %s",(map == core ? "core" : "alt"));
  1091.     first = 1;
  1092.     *linebuf = '\0';
  1093.     for (mp = *map; mp; mp = mp->next) {
  1094.     for (loc = mp->loc; loc < (mp->loc + mp->len); loc += EIGHT_K) {
  1095.         if (first || ((loc & 0x1ffff) == 0)) {
  1096.         if (*linebuf) FORCE(linebuf);
  1097.         ksprintf(linebuf,"\r%08lx: ",loc);
  1098.         lp = &linebuf[11];
  1099.         first = 0;
  1100.         }
  1101.         if (loc == mp->loc) {
  1102.         *lp++ = modesym[global_mode_table[loc / EIGHT_K]];
  1103.  
  1104.         for (p = proclist; p; p = p->gl_next) {
  1105.             if (p->mem) {
  1106.             mr = p->mem;
  1107.             for (i=0; i < p->num_reg; i++, mr++) {
  1108.                 if (*mr == mp) {
  1109.                 owner = p->pid;
  1110.                 goto gotowner;
  1111.                 }
  1112.             }
  1113.             }
  1114.         }
  1115.         owner = 000;
  1116. gotowner:
  1117.         ksprintf(lp,"%03d",owner);
  1118.         lp += 3;
  1119.         }
  1120.         else {
  1121.         *lp++ = ' ';
  1122.         *lp++ = '-';
  1123.         *lp++ = '-';
  1124.         *lp++ = '-';
  1125.         *lp = '\0';    /* string is always null-terminated */
  1126.         }
  1127.         }
  1128.     }
  1129.     FORCE(linebuf);
  1130.   }
  1131.  
  1132.     if (bigone) {
  1133.     long_desc tbl;
  1134.  
  1135.     /* fill in tbl with the only parts used at the top level */
  1136.     tbl.page_type.dt = proc->ctxt[CURRENT].crp.dt;
  1137.     tbl.tbl_address = proc->ctxt[CURRENT].crp.tbl_address;
  1138.     _dump_tree(tbl,0);
  1139.     }
  1140. #endif /* DEBUG_INFO */
  1141. }
  1142.  
  1143.  
  1144. /*
  1145.  * Can the process "p" access the "nbytes" long
  1146.  * block of memory starting at "start"?
  1147.  * If it would be a legal access, the current
  1148.  * process is given temporary access via
  1149.  * prot_temp.
  1150.  * Returns a cookie like the one prot_temp
  1151.  * returns; if the process shouldn't have
  1152.  * access to the memory, returns 1.
  1153.  *
  1154.  * BUG: should actually read p's page table to
  1155.  * determine access
  1156.  */
  1157.  
  1158. int
  1159. mem_access_for(p, start, nbytes)
  1160.     PROC *p;
  1161.     ulong start;
  1162.      long nbytes;
  1163. {
  1164.     MEMREGION **mr;
  1165.     int i;
  1166.  
  1167.     if (no_mem_prot) return -1;
  1168.     if (start >= (ulong)p && start+nbytes <= (ulong)(p+1))
  1169.         return -1;
  1170.     if (p == rootproc)
  1171.         goto win_and_mark;
  1172.  
  1173.     mr = p->mem;
  1174.     if (mr) {
  1175.         for (i = 0; i < p->num_reg; i++, mr++) {
  1176.         if (*mr) {
  1177.             if (((*mr)->loc <= start) &&
  1178.             ((*mr)->loc + (*mr)->len >= start + nbytes))
  1179.                 goto win_and_mark;
  1180.         }
  1181.         }
  1182.     }
  1183.  
  1184.     return 0;    /* we don't own this memory */
  1185.  
  1186. win_and_mark:
  1187.     return prot_temp(start, nbytes, -1);
  1188. }
  1189.